package com.bishe.utils;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class LayDataToJson {


    /**
     * 抛出json数据格式
     */
        public  String toJson(LayDataResult result, HttpServletResponse response) throws Exception{
            response.setContentType("text/json");
            response.setHeader("Cache-Control", "no-cache");
            response.setCharacterEncoding("UTF-8");

            String json = JSONObject.toJSONString(result,
                    // 保留 Map 空的字段
                    SerializerFeature.WriteMapNullValue,
                    //将 Number 类型的 null 转成 0
                    SerializerFeature.WriteNullNumberAsZero,
                    //将 List 类型的 null 转成 []
                    SerializerFeature.WriteNullListAsEmpty,
                    // 将 String 类型的 null 转成""
                    SerializerFeature.WriteNullStringAsEmpty,
                    // 将 Boolean 类型的 null 转成 false
                    SerializerFeature.WriteNullBooleanAsFalse,
                    // 避免循环引用
                    SerializerFeature.DisableCircularReferenceDetect);
            return json;
        }


}
